package cn.ante.org.controller;

import cn.ante.basic.annotation.PreAuthorize;
import cn.ante.basic.exception.BusinessException;
import cn.ante.basic.utils.ExcelUtil;
import cn.ante.org.domain.ShopAuditLog;
import cn.ante.org.service.IShopService;
import cn.ante.org.domain.Shop;
import cn.ante.org.query.ShopQuery;
import cn.ante.basic.utils.AjaxResult;
import cn.ante.basic.utils.PageList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(value = "店铺的api",description="店铺相关的crud功能")
public class ShopController {
    @Autowired
    public IShopService shopService;

    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @ApiOperation(value = "新增/修改店铺信息")
    @PreAuthorize(name ="新增/修改店铺信息",sn="shop:addOrUpdate")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop) {
        try {
            if (shop.getId() != null) {
                shopService.update(shop);
            } else {
                shopService.add(shop);
            }
            return AjaxResult.me();
        } catch(Exception e){
                e.printStackTrace();
                return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！" + e.getMessage());
            }

    }
    /**
    * 删除对象信息
    * @param :id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @ApiOperation(value = "根据id删除店铺信息")
    @PreAuthorize(name ="根据id删除店铺信息",sn="shop:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取店铺信息")
    @PreAuthorize(name ="根据id查询",sn="shop:get")
    public Shop get(@PathVariable("id")Long id) {
        return shopService.loadById(id);
    }


    // 导出店铺信息
    @GetMapping("/export/excel")
    @ApiOperation(value = "导出店铺信息")
    @PreAuthorize(name ="导出店铺信息",sn="shop:exportExcel")
    public void exportExcel(HttpServletResponse response) {
        List<Shop> shops = shopService.loadAll();
        ExcelUtil.exportExcel(shops,null,"店铺信息",Shop.class,"店铺信息.xlsx",response);
    }

    // 导入店铺信息
    @PostMapping("/import/excel")
    @ApiOperation(value = "导入店铺信息")
    @PreAuthorize(name ="导入店铺信息",sn="shop:importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file) {
        List<Shop> shops = ExcelUtil.importExcel(file,0,1,Shop.class);
        // 调用批量新增
//        for (Shop shop: shops) {
//            shopService.batchAdd(Arrays.asList(shop));
//        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){
        return shopService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    @ApiOperation(value = "分页查询店铺信息")
    @PreAuthorize(name ="分页查询店铺",sn="shop:list")
    public PageList<Shop> findAll(@RequestBody ShopQuery query)
    {
        return shopService.findAll(query);
    }

    /* 审核入驻接口 */
    @PostMapping("/settlement")
    public AjaxResult json(@RequestBody Shop shop) {
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());

        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

    /* 审核情况接口 审核驳回 */

    @PostMapping("/audit/reject")
    @ApiOperation(value = "审核店铺信息")
    @PreAuthorize(name ="审核店铺信息",sn="shop:audit")
    public AjaxResult auditReject( @RequestBody ShopAuditLog shopAuditLog) {
        try{
        shopService.auditReject(shopAuditLog);
        return AjaxResult.me();
    }catch (BusinessException e){
        e.printStackTrace();
        return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());

    }catch (Exception e){
        e.printStackTrace();
        return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
    }
    }

    /* 审核情况接口 审核拒绝 */
    @PostMapping("/audit/refuse")
    @ApiOperation(value = "审核店铺信息")
    @PreAuthorize(name ="审核店铺信息",sn="shop:audit")
    public AjaxResult rejectAlway(@RequestBody ShopAuditLog shopAuditLog) {
        try{
            shopService.rejectAlway(shopAuditLog);
            return AjaxResult.me();
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());

        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }

    }

    /* 审核情况接口 审核通过 */
    @PostMapping("/audit/pass")
    @ApiOperation(value = "审核店铺信息")
    @PreAuthorize(name ="审核店铺信息",sn="shop:audit")
    public AjaxResult auditPass(@RequestBody ShopAuditLog shopAuditLog) {
        try{
            shopService.auditPass(shopAuditLog);
            return AjaxResult.me();
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());

        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

}
